#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import render_template, request, Blueprint, jsonify
from my_app.create_photo.create import write_image
from my_app.create_photo.id_number import CalculateTime
from my_app.create_photo.constant import AREA_INFO
from flask_login import login_required


create_photo = Blueprint("create_photo", __name__)


@login_required
@create_photo.route("/ajax-photo-id", methods=["POST", "GET"])
def id_photo():
    """证件照"""
    all_mess = request.form.get("all_mess")
    if all_mess:
        all_mess = [[int(j) if j.isdigit() else j for j in i] for i in eval(all_mess)]
        mess_data = []
        for n, mess in enumerate(all_mess):
            s_t = CalculateTime(t=mess[3])
            mess_data.append(
                write_image(sex=mess[1], age=mess[0], f=mess[2], is_create_photo=True,
                            s_t=s_t, area=mess[4]))
            mess_data[n]['effective'] = f"T+{s_t.t} {s_t.start_time.strftime(s_t.date_format)}"  # 生效日
        return jsonify({"status": 200, "data": mess_data, "message": "哈哈，您成功了~"})
    return render_template("create_photo/ajax-photo-id.html", areas=AREA_INFO)

